#ifndef CONDITIONAL_THREAD_H_
#define CONDITIONAL_THREAD_H_

#include "Thread.h"

class ConditionalThread : public Thread
{
private:
    HANDLE m_event;
    HANDLE m_isWaitingEvent;
    bool m_pauseRequested;
public:
    ConditionalThread() : m_pauseRequested(true)
    {
        m_event = CreateEvent(0,true, FALSE, NULL);
        m_isWaitingEvent = CreateEvent(0,FALSE,FALSE,NULL);
    }
    ~ConditionalThread()
    {
        CloseHandle(m_event);
    }
    virtual void vRun()
    {
        while(m_tActive)
        {
            while(m_pauseRequested)
            {
                SignalObjectAndWait(m_isWaitingEvent,m_event, INFINITE,false);
            }
            vProcess();
        }
    }
    virtual void vStop()
    {
        m_tActive = false;
    }
    virtual void vPause()
    {
        m_pauseRequested = true;
        ResetEvent(m_event);
    }
    virtual void vResume()
    {
        m_pauseRequested = false;
        SetEvent(m_event);
    }
    virtual int WaitForFinish()
    {
        WaitForSingleObject(m_isWaitingEvent, INFINITE);
        return GetExitCode();
    }
};

#endif